package com.kyzh.core.pager.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.beans.HomeV3MultiItem
import com.gushenge.core.beans.IndexTop
import com.gushenge.core.requests.HomeRequest
import com.kyzh.core.R
import com.kyzh.core.adapters.BiaoqianAdapter
import com.kyzh.core.adapters.v3.GameListBorderAdapter
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.startGameDetailActivity
import com.gushenge.core.toast
import com.kyzh.core.utils.setVisibility
import com.makeramen.roundedimageview.RoundedImageView
import kotlinx.android.synthetic.main.activity_game_detail2.*
import kotlinx.android.synthetic.main.fragment_homeothers.*


class HomeOthersFragment : BaseFragment() {


    private var data: IndexTop?=null
    companion object {
        @JvmStatic
        fun getNewInstance(data: IndexTop): HomeOthersFragment {
            val fragment = HomeOthersFragment()
            fragment.arguments = bundleOf("data" to data)
            return fragment
        }
    }

    private val beans = arrayListOf<HomeV3MultiItem>()
    private val adapter = Adapter(beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        data = arguments?.getSerializable("data")  as IndexTop
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
         return inflater.inflate(R.layout.fragment_homeothers, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        srlRoot.setOnRefreshListener {
            initData()
        }
        rvOthers.adapter = adapter
        rvOthers.setItemViewCacheSize(50)
        initData()
    }

    override fun onResume() {
        super.onResume()
        if (beans.isEmpty()){
            initData()
        }
    }

    private fun initData(){
        HomeRequest.others(1,data?.type?:2) {
            beans.clear()
            beans.add(HomeV3MultiItem(gameTop =  data?.name+getString(R.string.billboard),tuijianOther = top[0],game1 = top.also { it.removeAt(0) },style = 0))
            beans.add(HomeV3MultiItem(game1 = list,style = 1))
            this@HomeOthersFragment.p = this.p
            this@HomeOthersFragment.max = this.max_p
            adapter.notifyDataSetChanged()
        }

        srlRoot.finishRefresh()
    }

    private inner class Adapter(beans:ArrayList<HomeV3MultiItem>): BaseMultiItemQuickAdapter<HomeV3MultiItem, BaseViewHolder>(beans){

        init {
            addItemType(0,R.layout.item_home_recom_game)
            addItemType(1,R.layout.item_home_recom_sort)
        }

        override fun convert(holder: BaseViewHolder, item: HomeV3MultiItem) {
            when(holder.itemViewType){
                0->{
                    val icon: ImageView = holder.getView(R.id.ivGameIcon)
                    val topBg: RoundedImageView = holder.getView(R.id.ivTopBg)
                    val name: TextView = holder.getView(R.id.tvGameName)
                    val type: TextView = holder.getView(R.id.tvGameType)
                    val cl1: ConstraintLayout = holder.getView(R.id.cl1)
                    val tvTitle: TextView = holder.getView(R.id.tv1)
                    val tvDetail: TextView = holder.getView(R.id.tvDetail)
                    val bq: RecyclerView = holder.getView(R.id.bq)
                    val rvList: RecyclerView = holder.getView(R.id.rvList)
                    val discount: ArcButton = holder.getView(R.id.btDiscount)
                    icon.loadImage(item.tuijianOther.icon)
                    topBg.loadImage(item.tuijianOther.image)
                    name.text = item.tuijianOther.name
                    tvTitle.text = item.gameTop
                    type.text = "${item.tuijianOther.point}  ${item.tuijianOther.type}"
                    bq.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                    bq.adapter = BiaoqianAdapter( item.tuijianOther.biaoqian)
                    tvDetail.setOnClickListener {
                        startGameDetailActivity(item.tuijianOther.gid)
                    }

                    if(item.tuijianOther.zhekou.isNullOrEmpty()){
                        discount.setVisibility(false)
                    }else{
                        if(!item.tuijianOther.zhekou.contains("折")){
                            discount.text = item.tuijianOther.zhekou+"折"
                        }else{
                            discount.text = item.tuijianOther.zhekou
                        }
                        discount.setVisibility(true)
                        if(item.tuijianOther.zhekou.contains("10")){
                            discount.setVisibility(false)
                        }
                    }


//                    if (context.getString(R.string.zhekou) == item.tuijianOther.zhekou ||  item.tuijianOther.zhekou.isEmpty()){
//                        discount.visibility = View.GONE
//                    }else{
//                        discount.visibility = View.VISIBLE
//                        discount.text = item.tuijianOther.zhekou
//                    }
                    topBg.setOnClickListener {
                        startGameDetailActivity(item.tuijianOther.gid)
                    }
                    cl1.setOnClickListener {
                        startGameDetailActivity(item.tuijianOther.gid)
                    }
                    rvList.layoutManager = object : LinearLayoutManager(requireContext()){
                        override fun canScrollHorizontally(): Boolean {
                            return false
                        }

                        override fun canScrollVertically(): Boolean {
                            return false
                        }
                    }
                    rvList.adapter = GameListBorderAdapter(R.layout.item_game_border,item.game1)
                }
                1->{
                    val tv1: TextView = holder.getView(R.id.tv1)
                    val tvMore: TextView = holder.getView(R.id.tvMore)
                    tvMore.visibility = View.GONE
                    tv1.text = getString(R.string.allGame)
                    val rvList: RecyclerView = holder.getView(R.id.rvList)
                    rvList.layoutManager = object : LinearLayoutManager(requireContext()){
                        override fun canScrollHorizontally(): Boolean {
                            return false
                        }

                        override fun canScrollVertically(): Boolean {
                            return false
                        }
                    }
                    val beans = item.game1
                    val adapter = GameListBorderAdapter(R.layout.item_game_border,beans)
                    rvList.adapter = adapter

                    srlRoot.setEnableLoadMore(true)
                    srlRoot.setEnableAutoLoadMore(true)
                    srlRoot.setOnLoadMoreListener {
                        if (max<p){
                            getString(R.string.noHaveMore).toast()
                            srlRoot.finishLoadMore()
                        }else{
                            HomeRequest.others(p,this@HomeOthersFragment.data?.type?:2) {
                                this@HomeOthersFragment.p = p
                                this@HomeOthersFragment.max = max_p
                                adapter.addData(list)
                                srlRoot.finishLoadMore()
                            }
                        }
                    }
                }
            }
        }
    }


}
